package com.ruoyi.evaluate.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.evaluate.mapper.EvaluateNormMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.evaluate.domain.EvaluateNorm;
import com.ruoyi.evaluate.service.IEvaluateNormService;

/**
 * 评价指标Service业务层处理
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Service
public class EvaluateNormServiceImpl implements IEvaluateNormService 
{
    @Autowired
    private EvaluateNormMapper evaluateNormMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询评价指标
     * 
     * @param id 评价指标ID
     * @return 评价指标
     */
    @Override
    public EvaluateNorm selectEvaluateNormById(Long id)
    {
        return evaluateNormMapper.selectEvaluateNormById(id);
    }

    /**
     * 查询评价指标列表
     * 
     * @param evaluateNorm 评价指标
     * @return 评价指标
     */
    @Override
    public List<EvaluateNorm> selectEvaluateNormList(EvaluateNorm evaluateNorm)
    {
        return evaluateNormMapper.selectEvaluateNormList(evaluateNorm);
    }

    /**
     * 新增评价指标
     * 
     * @param evaluateNorm 评价指标
     * @return 结果
     */
    @Override
    public int insertEvaluateNorm(EvaluateNorm evaluateNorm)
    {
    	redisCache.deleteObject("norm");
        return evaluateNormMapper.insertEvaluateNorm(evaluateNorm);
    }

    /**
     * 修改评价指标
     * 
     * @param evaluateNorm 评价指标
     * @return 结果
     */
    @Override
    public int updateEvaluateNorm(EvaluateNorm evaluateNorm)
    {
    	redisCache.deleteObject("norm");
        return evaluateNormMapper.updateEvaluateNorm(evaluateNorm);
    }

    /**
     * 批量删除评价指标
     * 
     * @param ids 需要删除的评价指标ID
     * @return 结果
     */
    @Override
    public int deleteEvaluateNormByIds(Long[] ids)
    {
    	redisCache.deleteObject("norm");
        return evaluateNormMapper.deleteEvaluateNormByIds(ids);
    }

    /**
     * 删除评价指标信息
     * 
     * @param id 评价指标ID
     * @return 结果
     */
    @Override
    public int deleteEvaluateNormById(Long id)
    {
    	redisCache.deleteObject("norm");
        return evaluateNormMapper.deleteEvaluateNormById(id);
    }

    /**
     * 查询可评价指标列表
     * 
     * @return 评价指标集合
     */
    @Override
    public List<EvaluateNorm> getEvaluateNorms(){
    	List<EvaluateNorm> norms = redisCache.getCacheMapValue("norm", "norms");
    	if(norms != null) return norms;
    	
    	EvaluateNorm nc = new EvaluateNorm();
    	nc.setStatus("1");
    	norms = new ArrayList<>();
    	List<EvaluateNorm> list = evaluateNormMapper.selectEvaluateNormList(nc);
    	for(EvaluateNorm n:list) {
    		boolean f = false;
    		for(EvaluateNorm s:list) {
    			if(s.getPid().equals(n.getId())) {
    				f = true;
    			}
    		}
    		if(!f) {
    			norms.add(n);
    		}    		
    	}
    	redisCache.setCacheMapValue("norm", "norms", norms);
    	return norms;
    }
}
